package com.example.sudoku;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{
	
	//Chronometer cronometro;

	Sudoku sudoku = new Sudoku();
	Button btnSair, btnCronometro;
	TextView aviso;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnSair = (Button) findViewById(R.id.btnSair); //Referencia o botao Java com o xml
		
		/*cronometro = (Chronometer) findViewById(R.id.cronometro);
		
		btnCronometro = (Button) findViewById(R.id.btnCronometro);
		btnCronometro.setOnClickListener(new View.OnClickListener() {
			 @Override
			 public void onClick(View arg0) {
			   cronometro.setBase(SystemClock.elapsedRealtime());
			   cronometro.start();
			 }
			});*/
		
		referenciaBotoes(sudoku); //Referencia todos os botoes do sudoku
		sudoku.inicializa();  //Inicializa as strings dos botoes do sudoku
		
		aviso = (TextView) findViewById(R.id.aviso); //Referencia o TextView Java com o xml
        
        ouvirBotoes (sudoku, btnSair); //Listener de click de botoes
	}
	
	//Toda vez vem para esta funcao ao termino da TecladoActivity
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		aviso.setText("");  //inicializa o TextView com uma string vazia a cada retorno da TecladoActivity
		
		//Eu nao faco verificacao do requestCode, mas eu o utilizo no codigo abaixo para me dizer qual botaodo
		//vetor de botoes eu irei alterar o valor
		//if (requestCode == 0) { 
		if(resultCode == RESULT_OK){
			//Coloca um novo texto no botao, o texto colocado eh o que foi escolhido belo botao da TecladoActivity
			sudoku.Sudoku[requestCode].setText(data.getStringExtra("result"));
			
			//verifica se tem numero repetido na linha e/ou na coluna
			if(sudoku.repetiuNaLinha(requestCode) && sudoku.repetiuNaColuna(requestCode)){
				aviso.setText("Mas hein, o numero "+sudoku.Sudoku[requestCode].getText()+" esta repetido na linha e na coluna.");
			}
			else if (sudoku.repetiuNaLinha(requestCode)){
				aviso.setText("Mas hein, ja tem um "+sudoku.Sudoku[requestCode].getText()+" nesta linha.");
			}
			else if (sudoku.repetiuNaColuna(requestCode)){
				aviso.setText("Mas hein, ja tem um "+sudoku.Sudoku[requestCode].getText()+" nesta coluna.");
			}
		}
		if (resultCode == RESULT_CANCELED) {/*If canceled, do nothing*/}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	void referenciaBotoes(Sudoku sudoku){
		//Primeira Linha
		sudoku.Sudoku[0] = (Button) findViewById(R.id.c11);
		sudoku.Sudoku[1] = (Button) findViewById(R.id.c12);
		sudoku.Sudoku[2] = (Button) findViewById(R.id.c13);
		sudoku.Sudoku[3] = (Button) findViewById(R.id.c14);
		sudoku.Sudoku[4] = (Button) findViewById(R.id.c15);
		sudoku.Sudoku[5] = (Button) findViewById(R.id.c16);
		sudoku.Sudoku[6] = (Button) findViewById(R.id.c17);
		sudoku.Sudoku[7] = (Button) findViewById(R.id.c18);
		sudoku.Sudoku[8] = (Button) findViewById(R.id.c19);

		//Segunda Linha
		sudoku.Sudoku[9] = (Button) findViewById(R.id.c21);
		sudoku.Sudoku[10] = (Button) findViewById(R.id.c22);
		sudoku.Sudoku[11] = (Button) findViewById(R.id.c23);
		sudoku.Sudoku[12] = (Button) findViewById(R.id.c24);
		sudoku.Sudoku[13] = (Button) findViewById(R.id.c25);
		sudoku.Sudoku[14] = (Button) findViewById(R.id.c26);
		sudoku.Sudoku[15] = (Button) findViewById(R.id.c27);
		sudoku.Sudoku[16] = (Button) findViewById(R.id.c28);
		sudoku.Sudoku[17] = (Button) findViewById(R.id.c29);
	}
	
	void ouvirBotoes (Sudoku sudoku, Button btnSair){
        
        btnSair.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                finish();  
            }  
        });
		
        //Primeira Linha
        sudoku.Sudoku[0].setOnClickListener (new View.OnClickListener () {
        	public void onClick (View v) {
	    		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
	    		startActivityForResult(i, 0);
    		}
        });
        sudoku.Sudoku[1].setOnClickListener (new View.OnClickListener () {
        	public void onClick (View v) {
        		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
        		startActivityForResult(i, 1);
        	}
        });
        sudoku.Sudoku[2].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 2);
     	   	}
        });
        sudoku.Sudoku[3].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 3);
     	   	}
        });
        sudoku.Sudoku[4].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 4);
     	   	}
        });
        sudoku.Sudoku[5].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 5);
     	   	}
        });
        sudoku.Sudoku[6].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 6);
     	   	}
        });
        sudoku.Sudoku[7].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 7);
     	   	}
        });
        sudoku.Sudoku[8].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 8);
     	   	}
        });
        
        //Segunda Linha
        sudoku.Sudoku[9].setOnClickListener (new View.OnClickListener () {
        	public void onClick (View v) {
	    		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
	    		startActivityForResult(i, 9);
    		}
        });
        sudoku.Sudoku[10].setOnClickListener (new View.OnClickListener () {
        	public void onClick (View v) {
        		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
        		startActivityForResult(i, 10);
        	}
        });
        sudoku.Sudoku[11].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 11);
     	   	}
        });
        sudoku.Sudoku[12].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 12);
     	   	}
        });
        sudoku.Sudoku[13].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 13);
     	   	}
        });
        sudoku.Sudoku[14].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 14);
     	   	}
        });
        sudoku.Sudoku[15].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 15);
     	   	}
        });
        sudoku.Sudoku[16].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 16);
     	   	}
        });
        sudoku.Sudoku[17].setOnClickListener (new View.OnClickListener () {
     	   	public void onClick (View v) {
     	   		Intent i = new Intent(MainActivity.this, TecladoActivity.class);
     	   		startActivityForResult(i, 17);
     	   	}
        });
        
	}

}
